<template>
  <div class="honor-tree-container">
    <div class="department-tree">
      <el-input placeholder="输入部门或者姓名进行过滤" v-model="departmentName"></el-input>
      <!-- default-expand-all :default-expanded-keys="[1, 4, 5]" -->
      <el-tree
        class="filter-tree"
        :data="departmentTreeNodes"
        :props="defaultProps"
        node-key="id"
        :default-expanded-keys="[100]"
        :filter-node-method="filterNode"
        @node-click="handleNodeClick"
        ref="departmentTree"
      ></el-tree>
    </div>

    <div class="honor-container">
      <el-form :inline="true" :model="formInline" ref="queryForm" class="demo-form-inline">
        <el-form-item label="荣誉名称">
          <el-input v-model="formInline.user" size="small" placeholder="荣誉名称"></el-input>
        </el-form-item>
        <el-form-item label="荣誉级别">
          <el-select v-model="formInline.region" size="small" placeholder="荣誉级别">
            <el-option label="学校" value="shanghai"></el-option>
            <el-option label="部门" value="beijing"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="种类">
          <el-input v-model="formInline.user" size="small" placeholder="种类"></el-input>
        </el-form-item>
        <el-form-item label="获奖时间">
          <el-date-picker
            v-model="dateRange"
            size="small"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="honorList" border style="width: 100%" class="honor-table">
        <el-table-column prop="awardedAt" label="获得日期" width="180"></el-table-column>
        <el-table-column prop="winner" label="获得人" width="180"></el-table-column>
        <!-- prop="honorName" -->
        <el-table-column label="荣誉名称" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <router-link
              :to="'/honor/honor/edit/' + scope.row.honorId"
              target="_blank"
              class="link-type"
            >
              <span>{{ scope.row.honorName }}</span>
            </router-link>
          </template>
        </el-table-column>
        <el-table-column prop="awardedBy" label="颁奖单位"></el-table-column>
        <el-table-column prop="category" label="种类"></el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="loadHonors"
      />
    </div>
  </div>
</template>

<script>
import { getHonor, listHonor, addHonor, updateHonor } from "@/api/archive-honor/honor";
// import { listUser, getUser, delUser, addUser, updateUser, exportUser, resetUserPwd, changeUserStatus, importTemplate } from "@/api/system/user";
import { treeselect } from "@/api/system/dept";

import HonorInfo from './components/honor-info'
import HonorDescription from './components/honor-desc'
import HonorPicture from './components/honor-picture.vue'
export default {
  name: 'HonorTree',
  components: {
    HonorInfo,
    HonorDescription,
    HonorPicture
  },

  data () {
    return {
      loading: true,
      departmentTreeNodes: [],
      // 用户表格数据
      // userList: [],
      honorList: [],
      // 总条数
      total: 0,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined
      },
      defaultProps: {
        children: "children",
        label: "label"
      },
      activeIndex: '0',
      honorModel: {},
      pictureLoaded: false,
      honorFormRules: {
        honorName: [
          { required: true, message: '请输入活商品动名称', trigger: 'blur' },
          { min: 2, max: 255, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
      departmentName: '',
      defaultProps: {
        children: 'children',
        label: 'label',
        id: 'id'
      },
      formInline: {
        user: '',
        region: ''
      },
    }
  },
  watch: {
    departmentName (val) {
      this.$refs.departmentTree.filter(val);
    }
  },
  methods: {
    filterNode (value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 查询部门下拉树结构
    loadDepartmentTreeNodes () {
      treeselect().then(response => {
        this.departmentTreeNodes = response.data;
      });
    },
    loadHonors () {
      this.loading = true;
      listHonor(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.honorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      // honorList
    },
    // 节点单击事件
    handleNodeClick (data) {
      this.queryParams.deptId = data.id;
      this.loadHonors();
    },
    handleEdit () {
      return;
    },
    onAddPicture (picture) {
      console.log('添加图片：', picture)
    },
    onDeletePicture (honorPictureId) {
      console.log('删除图片：', honorPictureId)
    },
    onSwapPicture (pictures) {
      console.log('移动图片：', pictures)
    },
    beforeTabLeave (activeName, oldActiveName) {
      // console.log('activeName, oldActiveName', activeName, oldActiveName)
      if (!this.pictureLoaded && activeName === '3') {
        this.$refs.honorPicture.loadPictures(this.honorModel.honorId)
        this.pictureLoaded = true
        console.log('加载了图片', this.honorModel.honorId)
      }
    },
    async handleUpdate (honorId) {
      const { data: honor } = await getHonor(honorId)

      honor.pictureList = []
      this.honorModel = honor;
      // const {data: honorPictures} = await getHonorPictures({honorId: honorId})
      // // console.log('honorPictures:', honorPictures)
      // // 这里将 获取图片放到 图片 tabItem 中
      // this.honorModel.pictureList = honorPictures
      // console.log('父组件获取数据成功:', this.honorModel)
    },
    handleQuery () {
      // console.log('mounted') honorFormRef
      console.log('honor edit onSubmit:', this.honorModel)
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    }
  },
  mounted () {
    // console.log('HonorEdit mounted')
  },
  // async created () {
  //   // console.log('HonorEdit created')
  //   // await this.handleUpdate(this.$route.params.honorId)
  // },
  created () {
    this.loadHonors();
    this.loadDepartmentTreeNodes();
  },


}
</script>

<style scoped>
.save-btn {
  margin: 20px 105px;
}
.honor-tree-container {
  display: flex;
}
.honor-tree-container .department-tree {
  flex-basis: 200px;
  width: 200px;
}
.honor-tree-container .department-tree,
.honor-tree-container .honor-container {
  margin-top: 20px;
  margin-left: 20px;
}
/deep/ .el-input--small {
  width: 120px;
}
</style>